/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbenglish_client.form;

import bllInterface.Account;
import bllfactory.SBLLFactory;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import model.ClientFriendListModel;
import model.ClientModel;
import model.chat.ChatMessageModel;

/**
 *
 * @author Mrkupi
 */
public class userInfo extends javax.swing.JFrame implements ListDataListener
{
    ClientFriendListModel   model;
    DefaultListModel newModel   =   new DefaultListModel();
    ArrayList<String> strModel  =   new ArrayList();    
    /**
     * Creates new form userInfo
     */
    public userInfo() {
        initComponents();
        
        this.model  =   new ClientFriendListModel();
        model.addListDataListener(this);
        
        init();
        onLoadInfo();
        onLoadFriendList();
    }

    private void init()
    {
        setLocation( 400, 100 );
        setResizable(false);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               dispose();
            }
        });
    }
    
    private void createFriendList()
    {
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                e.getWindow().dispose();
            }
        });
        
        scrollPaneFriendlist = new javax.swing.JScrollPane();
        friendList  =   new javax.swing.JList( model ){
             @Override
             public String getToolTipText(MouseEvent e)
             {
                 int index = locationToIndex(e.getPoint());
                 if (-1 < index) 
                 {
                     return strModel.get( index );
                 } 
                 else 
                 {
                     return null;
                 }
             }
        };
        scrollPaneFriendlist.setViewportView(friendList);
        friendList.setLocation( 170, 190);
        friendList.setVisible( true );
        friendList.setSize(400, 250);
        friendList.setBorder(BorderFactory.createLineBorder(Color.black));
        friendList.addMouseListener( new MouseAdapter(){
            @Override
            public void mousePressed( MouseEvent e )
            {
            }
            
            @Override
            public void mouseClicked( MouseEvent e )
            {
                if (e.getClickCount() == 2)
                {
                    Object selected =   friendList.getSelectedValue();
                    String strRoom  =   (String) selected;
                    if( !strRoom.equals("") )
                    {
                        createPrivateChat( strRoom, strRoom );
                    }
                }
            }
        } );
        this.add( friendList );
    }
    
    public void createPrivateChat( String id, String name ){
        if( !RoomContainer.Container.containsKey(id) )
        {
            ChatMessageModel chatMsgModel = new ChatMessageModel();
            ClientModel.getInstance().getChatMsgModels().put(id, chatMsgModel);
            privateChat pc = new privateChat(chatMsgModel, id, name);
            pc.setVisible(true);
            RoomContainer.Container.put(id, pc);
        }
    }
    
    private void onLoadInfo()
    {
        createFriendList();
        try 
        {
            this.lblNickName.setText( ClientModel.getInstance().getAccount().getUserName() );
            this.lblExp.setText( String.valueOf( ClientModel.getInstance().getAccount().getExp() ) );
            this.lblLevel.setText( String.valueOf( ClientModel.getInstance().getAccount().getLevel() ) );
        }
        catch (RemoteException ex) 
        {
            Logger.getLogger(userInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void onLoadFriendList()
    {        
        try 
        {
            // Querry friend list
            ArrayList<Account> accs    =   SBLLFactory.getInstance().getRelation().getFriendList( ClientModel.getInstance().getAccount().getUserID() );
            
            for( int i = 0; i < accs.size(); ++i )
            {
                this.model.addAccount( accs.get( i ) );
            }
        } 
        catch (RemoteException ex) 
        {
            Logger.getLogger(menu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnAvatar = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        pnInfo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblLevel = new javax.swing.JLabel();
        lblExp = new javax.swing.JLabel();
        lblDataJoin = new javax.swing.JLabel();
        lblNickName = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btChangePassword = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("USER INFO");

        pnAvatar.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton2.setIcon(new javax.swing.ImageIcon("E:\\Projects\\Java\\tbenglish\\TBEnglish_Client\\src\\tbenglish_client\\form\\Image\\HEINS_DESKTOP_LOOKUPS BRUCE.png")); // NOI18N

        javax.swing.GroupLayout pnAvatarLayout = new javax.swing.GroupLayout(pnAvatar);
        pnAvatar.setLayout(pnAvatarLayout);
        pnAvatarLayout.setHorizontalGroup(
            pnAvatarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnAvatarLayout.setVerticalGroup(
            pnAvatarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
        );

        pnInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Level");

        jLabel2.setText("Exp");

        jLabel3.setText("Date join");

        jLabel4.setText("Nick name");

        lblLevel.setText("jLabel5");

        lblExp.setText("jLabel6");

        lblNickName.setText("jLabel8");

        javax.swing.GroupLayout pnInfoLayout = new javax.swing.GroupLayout(pnInfo);
        pnInfo.setLayout(pnInfoLayout);
        pnInfoLayout.setHorizontalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLevel)
                    .addComponent(lblExp)
                    .addComponent(lblDataJoin)
                    .addComponent(lblNickName))
                .addContainerGap(338, Short.MAX_VALUE))
        );
        pnInfoLayout.setVerticalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblLevel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblExp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblDataJoin))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblNickName))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Friend List");
        jLabel9.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btChangePassword.setText("Change Password");
        btChangePassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                onChangePassword_click(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btChangePassword, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(pnAvatar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnAvatar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btChangePassword)
                .addGap(0, 246, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
    private void onChangePassword_click(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onChangePassword_click
        ChangePassword changePW = new ChangePassword();
        changePW.setVisible(true);
        changePW.setResizable(false);
    }//GEN-LAST:event_onChangePassword_click

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {
        new userInfo().setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btChangePassword;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblDataJoin;
    private javax.swing.JLabel lblExp;
    private javax.swing.JLabel lblLevel;
    private javax.swing.JLabel lblNickName;
    private javax.swing.JPanel pnAvatar;
    private javax.swing.JPanel pnInfo;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JList friendList;
    private javax.swing.JScrollPane scrollPaneFriendlist;  
    
    @Override
    public void intervalAdded(ListDataEvent e) 
    {
        ClientFriendListModel cModel = (ClientFriendListModel) e.getSource();
        for (int i = e.getIndex0(); i < e.getIndex1(); ++i)
        {
            Account cm = (Account) cModel.getElementAt(i);
            try 
            {
                newModel.addElement(cm.getUserName());
                friendList.setModel( newModel );
                
                strModel.add("Level: " + cm.getLevel() + ", Exp: "+ cm.getExp() );
            } 
            catch (RemoteException ex) {
                Logger.getLogger(menu.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void intervalRemoved(ListDataEvent e) {
    }

    @Override
    public void contentsChanged(ListDataEvent e) {
    }
}
